package by.demchenko.sample.webapp.page.home.panel;

import by.demchenko.sample.services.SectionService;
import by.demchenko.sample.webapp.app.BasicAuthenticationSession;
import by.demchenko.sample.webapp.page.home.share.ContactsPage;
import by.demchenko.sample.webapp.page.home.share.CoursesPage;
import by.demchenko.sample.webapp.page.home.share.HomePage;
import by.demchenko.sample.webapp.page.home.share.LiteraturePage;
import by.demchenko.sample.webapp.page.home.students.GradesPage;
import by.demchenko.sample.webapp.page.home.students.StudentProfilePage;
import by.demchenko.sample.webapp.page.home.teachers.StatusPage;
import by.demchenko.sample.webapp.page.home.teachers.TeacherCoursesPage;
import by.demchenko.sample.webapp.page.home.teachers.TeacherProfilePage;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.authroles.authentication.AuthenticatedWebSession;
import org.apache.wicket.authroles.authorization.strategies.role.Roles;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;

import javax.inject.Inject;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class MenuPanel extends Panel {
    @Inject
    SectionService sectionService;

    private boolean loginFormVisibility = false;

    public static final String ROLE_STUDENT = "STUDENT";
    public static final String ROLE_TEACHER = "TEACHER";

    public MenuPanel(String id) {
        super(id);
    }

    @Override
    protected void onInitialize() {
        super.onInitialize();

        List<PageParam> allSections = new ArrayList<>();

        Roles roles = AuthenticatedWebSession.get().getRoles();

        if (AuthenticatedWebSession.get().isSignedIn()) {
            String buttonText = " [" + BasicAuthenticationSession.get().getUser().getAccount().getLogin().toLowerCase() + "]";
            if (roles.hasRole(ROLE_STUDENT)) {
                allSections.add(new PageParam(getString("menu.main"), HomePage.class));
                allSections.add(new PageParam(getString("menu.faculties"), CoursesPage.class));
                allSections.add(new PageParam(getString("menu.grades"), GradesPage.class));
                allSections.add(new PageParam(getString("menu.clientArea"), StudentProfilePage.class));
                allSections.add(new PageParam(getString("menu.logout") + buttonText, null));
            } else if (roles.hasRole(ROLE_TEACHER)) {
                allSections.add(new PageParam(getString("menu.main"), HomePage.class));
                allSections.add(new PageParam(getString("menu.grades"), TeacherCoursesPage.class));
                allSections.add(new PageParam(getString("menu.status"), StatusPage.class));
                allSections.add(new PageParam(getString("menu.clientArea"), TeacherProfilePage.class));
                allSections.add(new PageParam(getString("menu.logout") + buttonText, null));
            }
        } else {
            allSections.add(new PageParam(getString("menu.main"), HomePage.class));
            allSections.add(new PageParam(getString("menu.faculties"), CoursesPage.class));
            allSections.add(new PageParam(getString("menu.literature"), LiteraturePage.class));
            allSections.add(new PageParam(getString("menu.contacts"), ContactsPage.class));
            allSections.add(new PageParam(getString("menu.login"), LoginForm.class));
        }


        final LoginForm loginform = new LoginForm("loginform");
        loginform.add(new AttributeModifier("class", new Model<>(loginFormVisibility ? "loginvisible" : "logininvisible")));
        loginform.setOutputMarkupId(true);
        add(loginform);

        ListView<PageParam> list = new ListView<PageParam>("menuItems", allSections) {
            @Override
            protected void populateItem(ListItem<PageParam> item) {
                PageParam param = item.getModelObject();
                if (param.getPageClass() == LoginForm.class) {
                    AjaxLink<Void> link = new AjaxLink<Void>("menuItemLink") {
                        @Override
                        public void onClick(AjaxRequestTarget target) {
                            loginFormVisibility = !loginFormVisibility;
                            loginform.add(new AttributeModifier("class", new Model<>(loginFormVisibility ? "loginvisible" : "logininvisible")));
                            target.add(loginform);
                        }
                    };
                    item.add(link);
                    String buttonText = param.getResName();
                    link.add(new Label("menuItem", buttonText));
                } else {
                    Link link = new Link<String>("menuItemLink") {
                        @Override
                        public void onClick() {
                            if (param.getPageClass() == null) {
                                BasicAuthenticationSession.get().signOut(this);
                            } else {
                                item.setResponsePage(param.getPageClass());
                            }
                        }
                    };
                    item.add(link);
                    link.add(new Label("menuItem", param.getResName()));
                }

            }
        };

        add(list);
    }

    private class PageParam implements Serializable {
        private String resName;

        public PageParam(String resName, Class pageClass) {
            this.resName = resName;
            this.pageClass = pageClass;
        }

        public String getResName() {

            return resName;
        }

        public Class getPageClass() {
            return pageClass;
        }

        private Class pageClass;
    }
}
